﻿#include <iostream>
#include <chrono>
#include <wiringPi.h>
#include "core/ThreadPool.hpp"
#include "utils/Logger.hpp"
#include "controller/MotorDriver.hpp"

#include "controller/WeaponController.hpp"
#include "event/EventManager.hpp"
#include "event/WeaponEvents.hpp"
#include <memory>


void turunOn(std::shared_ptr<WeaponController> launcher) {
    if (!launcher->initialize()) return;
    launcher->startFiring(); // 继电器闭合
    std::this_thread::sleep_for(std::chrono::milliseconds(5000)); // 模拟发射持续5秒
    launcher->stopFiring(); // 继电器断开
}

void motorSpeedTest(std::shared_ptr<MotorDriver> motorDriver, uint8_t addr) {

    // 使能电机
    motorDriver->enControl(addr, true, false); // 使能电机并禁用多机同步
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    // 速度模式
    motorDriver->velControl(addr, 0, 1000, 0, false);
    std::this_thread::sleep_for(std::chrono::seconds(10));
    motorDriver->stopNow(addr, false); // 停止电机
	std::this_thread::sleep_for(std::chrono::milliseconds(100));

	motorDriver->resetCurPosToZero(addr); // 清零当前位置
	std::this_thread::sleep_for(std::chrono::milliseconds(100));
	// 位置模式
	motorDriver->posControl(addr, 0, 1000, 0, 3200, false, false);

}

void getrealTimeAngle(std::shared_ptr<MotorDriver> motorDriver, uint8_t addr) {
    motorDriver->realTimeLocation(addr);
}

int main() {
    // 初始化日志系统
    auto logger = WaveGuard::Logger::getInstance();
    logger->addOutput(std::make_shared<WaveGuard::FileOutput>("waveguard.log"));
    logger->setLogLevel(WaveGuard::LogLevel::INFO); // 设置日志级别为INFO
    logger->setConsoleOutputEnabled(true); // 添加文件输出，同时输出到控制台和文件
    logger->setEnableThreadId(true); // 禁用线程ID输出（默认启用）

    // 初始化wiringPi库
    if (wiringPiSetup() == -1) LOG_ERROR("Failed to initialize wiringPi!");

    uint8_t motorAddr = 0x01; // 电机地址

    std::shared_ptr<ThreadPool> pool = std::make_shared<ThreadPool>(4, 10);
    LOG_INFO("The thread pool was successfully created.");

    // 初始化事件管理器
    auto eventManager = std::make_shared<WaveGuard::EventManager>(pool);
    // 初始化武器控制器
    auto weaponController = std::make_shared<WeaponController>();
    // 创建事件监听器
    auto listener = std::make_shared<WaveGuard::WeaponEventControllerListener>(weaponController);

    // 注册事件监听器
    eventManager->subscribe(WaveGuard::EventType::WEAPON_FIRE_START, listener);
    eventManager->subscribe(WaveGuard::EventType::WEAPON_FIRE_STOP, listener);

    // 发布开始发射事件
    auto startEvent = std::make_shared<WaveGuard::WeaponFireStartEvent>();
    eventManager->publishAsync(startEvent);

    // 处理事件
    eventManager->processEvents();

    // 发布停止发射事件
    auto stopEvent = std::make_shared<WaveGuard::WeaponFireStopEvent>();
    eventManager->publishAsync(stopEvent);

    // 处理事件
    eventManager->processEvents();

    std::shared_ptr<MotorDriver> motorDriver = std::make_shared<MotorDriver>("/dev/ttyAMA0");
    LOG_INFO("The serial port initialization was successful.");
    
    // 使用shared_ptr传递给线程池，确保引用计数正确
    auto motorTask = pool->enqueue(motorSpeedTest, motorDriver, motorAddr);
    std::this_thread::sleep_for(std::chrono::seconds(5));
    pool->enqueue(getrealTimeAngle, motorDriver, motorAddr);
    
    std::shared_ptr<WeaponController> weaponController = std::make_shared<WeaponController>();
    //pool->enqueue(turunOn, weaponController);

    // 关闭线程池（确保所有任务完成）
    pool.reset();

    return 0;
}